package com.tange.feature.device.query

import com.appbase.custom.base.ServiceBought
import com.tange.core.data.structure.Device
import com.tange.feature.data.structure.DeviceBasicResp
import com.tange.feature.data.structure.DeviceBatteryResp
import com.tange.feature.data.structure.DeviceLocationResp
import com.tg.data.bean.DeviceItem
import com.tg.data.http.entity.DeviceServiceStatusBean

/**
 * 将新接口的字段映射至老数据结构，这只是减少代码改动量的过渡逻辑，
 * 后续将废弃 DeviceItem
 */
fun convertBizDevice(deviceBasic: DeviceBasicResp): DeviceItem {

    val bizDevice = DeviceItem()

    deviceBasic.abilities?.let { bizDevice.abilities = it }
    deviceBasic.actived?.let { bizDevice.actived = try { it.toInt() } catch (e: Exception) { 0 } }
    deviceBasic.attributes?.let { bizDevice.attrs = it }
    deviceBasic.bindType?.let { bizDevice.bind_type = try { it.toInt() } catch (e: Exception) { 0 } }
    deviceBasic.city?.let { bizDevice.city = it }
    deviceBasic.connectWay?.let { bizDevice.connect_way = it }
    deviceBasic.country?.let { bizDevice.country = it }
    deviceBasic.versionName?.let { bizDevice.current_version = it }
    deviceBasic.versionCode?.let { bizDevice.current_version_code = it }
    deviceBasic.decryptKey?.let { bizDevice.des_key = it }
    deviceBasic.productType?.let { bizDevice.device_type = it }
    deviceBasic.firmwareId?.let { bizDevice.firmware_id = it }
    deviceBasic.hasLockPassword?.let { bizDevice.has_doorlock_pwd = it }
    deviceBasic.iccid?.let { bizDevice.iccid = it }
    deviceBasic.id?.let { bizDevice.id = it }
    deviceBasic.cover?.let { bizDevice.image_path = it }
    deviceBasic.isOnline?.let { bizDevice.is_online = try { it.toInt() } catch (e: Exception) { 0 } }
    deviceBasic.isOpen?.let { bizDevice.is_open = it }
    deviceBasic.isUpgrade?.let { bizDevice.is_upgrade = it }
    deviceBasic.name?.let { bizDevice.name = it }
    deviceBasic.notice?.let { bizDevice.notice = it }
    deviceBasic.p2pId?.let { bizDevice.p2p_id = it }
    deviceBasic.p2pPlatform?.let { bizDevice.p2p_platform = it }
    deviceBasic.p2pType?.let { bizDevice.p2p_type = it }
    deviceBasic.password?.let { bizDevice.password = it }
    deviceBasic.startTime?.let { bizDevice.start_time = try { it.toLong() } catch (e: Exception) { 0 } }
    deviceBasic.timezone?.let { bizDevice.timezone = it }
    deviceBasic.updateProgress?.let { bizDevice.update_progress = it }
    deviceBasic.updateStatus?.let { bizDevice.update_status = it }
    deviceBasic.uuid?.let { bizDevice.uuid = it }
    deviceBasic.iceServers?.let { bizDevice.iceServers = it }
    deviceBasic.gpsModel?.let { bizDevice.gps_model = it }
    deviceBasic.preconnect?.let { bizDevice.preconnect = it }
    deviceBasic.relay?.let { bizDevice.tg_relay = it }

    bizDevice.deviceBasicResp = deviceBasic

    return bizDevice
}

fun applyBattery(bizDevice: DeviceItem, deviceBatteryResp: DeviceBatteryResp) {
    bizDevice.deviceBatteryResp = deviceBatteryResp
    bizDevice.low_battery = deviceBatteryResp.lowBattery
    bizDevice.qoe = deviceBatteryResp.qoe
    bizDevice.charging = deviceBatteryResp.charging
}

fun applyLocation(bizDevice: DeviceItem, locationResp: DeviceLocationResp) {
    bizDevice.deviceLocationResp = locationResp
    bizDevice.lat = locationResp.latitude
    bizDevice.lng = locationResp.longitude
    bizDevice.speed = locationResp.speed
}

fun applyService(bizDevice: DeviceItem, r: DeviceServiceStatusBean) {
    bizDevice.deviceServiceResp = r

    bizDevice.should_show_super_ai = r.is_super_ai
    bizDevice.is_directional = r.is_directional
    bizDevice.device_notice = r.notice
    bizDevice.ai_server_data = r.ai
    bizDevice.car_server_data = r.car
    bizDevice.server_data = r.storage
    bizDevice.sim_server_data = r.sim
    bizDevice.foreignServiceHook = r.foreign_service_hook;

    // TODO: 废弃ServiceBought这个数据结构
    val serviceBought = ServiceBought().apply {
        ai_server = if (r.bought.ai) 1 else 0
        sim_server = if (r.bought.sim) 1 else 0
        car_server = if (r.bought.car) 1 else 0
        server = if (r.bought.storage) 1 else 0
    }
    bizDevice.server_bought = serviceBought
}